package net.libra.loader
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.system.ApplicationDomain;
	import flash.utils.ByteArray;
	
	import net.libra.loader.ILoader;
	import net.libra.loader.LoaderAssist;
	import net.libra.loader.LoadEvent;
	import net.libra.loader.mission.Mission;
	import net.libra.ui.Border;
	import net.libra.ui.base.BaseSprite;

	/**
	 * @author elvisjiang
	 */	
	public class Image extends BaseSprite
	{
		static private var cache:ILoader
		
		protected var loader:Loader;
		private var _path:String;
		private var _border:Border;
		private var _width:Number;
		private var _height:Number;
		private var _frame:uint;
		/**
		 * 没有width,height的时候 默认宽高跟图片的宽高一致.
		 * @param width 
		 * @param height
		 * @param border
		 * @param color
		 * @param backgroundColor
		 * 
		 */		
		public function Image(width:Number = NaN,height:Number = NaN,border:uint=0,color:uint=0,backgroundColor:uint=0x00000000)
		{
			if(!cache)
			{
				cache = new LoaderAssist("cw.image");
				cache.applicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain);
			}
			super();
			_width = width;
			_height = height;
			
			if(!isNaN(_width) && !isNaN(_height)){
		
				_border = new Border(width,height,color,border);
				_border.visible = false;
				addChild(_border);
			
				graphics.beginFill(backgroundColor,(backgroundColor >> 24 & 255)/255);
				graphics.drawRect(0,0,_width,_height);
				graphics.endFill();
			}else{
				_border = new Border(0,0,color,border);
				_border.visible = false;
				addChild(_border);
				
				graphics.beginFill(backgroundColor,(backgroundColor >> 24 & 255)/255);
				graphics.drawRect(0,0,0,0);
				graphics.endFill();
			}
		}
		public function load(path:String):void
		{
			if(_path == path)
			{
				return;
			}
			unload();
			
			_path = path;
			var b:ByteArray = cache.getBytes(_path);
			if(b)
			{
				_loadBytes(b);
				return;
			}
			cache.addEventListener(LoadEvent.MISSION_COMPLETE,_cacheComplete);
			cache.loadMission(new Mission(_path,_path,true,1,null));
		}
		private function _cacheComplete(e:LoadEvent):void
		{
			if(e.mission.path == _path)
			{
				cache.removeEventListener(LoadEvent.MISSION_COMPLETE,_cacheComplete);
				_loadBytes(e.mission.bytes);
			}
		}
		private function _loadBytes(bytes:ByteArray):void
		{
			if(bytes==null)return;
			_loaderLoad(bytes);
		}
		private function _loaderLoad(bytes:ByteArray):void
		{
			_loaderUnload();
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,_loaderComplete);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,_loaderComplete);
			loader.loadBytes(bytes);
		}
		private function _loaderUnload():void
		{
			if(loader)
			{
				if(loader.parent)loader.parent.removeChild(loader);
				loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,_loaderComplete);
				loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,_loaderComplete);

				try
				{
					loader.close();
				}catch(err:Error){};
				
				try
				{
					if(loader.hasOwnProperty("unloadAndStop"))
					{
						loader.unloadAndStop(true);
					}else
					{
						loader.unload();
					}
				}catch(err:Error){};
				
				loader = null;
			}
		}
		public function unload():void
		{
			cache.removeEventListener(LoadEvent.MISSION_COMPLETE,_cacheComplete);
			//if(!loader && !cache.hasBytes(_path))
			//{
			//	//cache.removeMission(_path,true,false);
			//}
			_border.visible = false;
			_path = null;
			_loaderUnload();
		}
		
		private function _show():void
		{
			Bitmap(loader.content).smoothing = true;
			
			if(!isNaN(_width) && !isNaN(_height)){
				loader.width = _width;
				loader.height = _height;
			}else{
				_width = loader.width;
				_height = loader.height;
			}
			addChildAt(loader,0);
			_border.visible = true;
			
			this.dispatchEvent(new LoadEvent(LoadEvent.ON_COMPLETE));
		}

		private function _loaderComplete(e:Event):void
		{
			if(_path==null)return;
			if(e.type==Event.COMPLETE)
			{
				_show();
			}else
			{
				Log.error("[IMAGE] ERROR" + _path);
			}
		}
		
		
		override public function get width():Number
		{
			return _width;
		}
		override public function get height():Number
		{
			return _height;
		}
		override public function destroy():void
		{
			cache.removeEventListener(LoadEvent.MISSION_COMPLETE,_cacheComplete);
			
			_path = null;
			
			unload();
			
			graphics.clear();
			
			_border.destroy();
			_border = null;
			super.destroy();
		}
	}
}